<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <router-link to="/">首页</router-link>
        <router-link to="/user">用户</router-link>
        <router-link to="/category">分类</router-link>
        <router-view></router-view>
    </div>
</body>
<script src="../lib/vue.js"></script>
<script src="./lib/vue-router.js"></script>
<script>
    // 组件
    var Index = {
        template: `<div>首页</div>`
    }
    var User = {
        template: `<div>用户</div>`
    }
    var Category = {
        template: `<div>分类</div>`
    }

    // 路由规则
    var router = new VueRouter({
        mode: 'history',
        routes: [
            {
                path: "/",
                component: Index
            },
            {
                path: "/user",
                component: User
            },
            {
                path: "/category",
                component: Category
            }
        ]
    })

    router.beforeEach(function (to, from, next) {
        //继续执行
        // next()
        // 停止
        // next(false)
        if (to.path === '/category') {
            next("/user")
        } else {
            next()
        }
    })

    new Vue({
        el: "#app",
        router
    })
</script>

</html>